﻿using System;
using System.ComponentModel;
using System.Drawing;
using System.IO;
using System.Windows.Forms;
using System.Xml;
using Autodesk.Revit.UI;
using YArchitech.LIB;
using YJKRevitDimensionText.DimCreator;
using YJKRevitDimensionText.Utility;

namespace YJKRevitDimensionText.Forms
{
	public partial class DlgDimensionHide : System.Windows.Forms.Form
	{
		public DlgDimensionHide(ExternalEvent exEvent, RequestHandler handler, string id, UIApplication uiapp)
		{
			this.InitializeComponent();
			this.m_Handler = handler;
			this.m_ExEvent = exEvent;
			this.viewID = id;
			this.uiapp = uiapp;
		}

		private void DlgDimensionHide_Load(object sender, EventArgs e)
		{
			//base.Icon = Resource.YArchitechIcon;
			DimensionHide dimensionHide = new DimensionHide(this.uiapp);
			if (!this.ReadData(dimensionHide))
			{
				this.ibtTwoPoint.ImageIndex = 0;
				this.ibtDoorWin.ImageIndex = 0;
				this.ibtWallThick.ImageIndex = 0;
				this.ibtAxis.ImageIndex = 0;
				dimensionHide.Excute(RequestId.ViewTwoPoint, false);
				dimensionHide.Excute(RequestId.ViewDoorWin, false);
				dimensionHide.Excute(RequestId.ViewWallThick, false);
				dimensionHide.Excute(RequestId.ViewAxis, false);
			}
		}

		private void btnTwoPoint_Click(object sender, EventArgs e)
		{
			this.MakeRequest(RequestId.SelectTwoPoint);
		}

		private void btnDoorWin_Click(object sender, EventArgs e)
		{
			this.MakeRequest(RequestId.SelectDoorWin);
		}

		private void btnWallThick_Click(object sender, EventArgs e)
		{
			this.MakeRequest(RequestId.SelectWallThick);
		}

		private void btnAxis_Click(object sender, EventArgs e)
		{
			this.MakeRequest(RequestId.SelectAxis);
		}

		private void ibtTwoPoint_Click(object sender, EventArgs e)
		{
			if (this.ibtTwoPoint.ImageIndex == 0)
			{
				this.ibtTwoPoint.ImageIndex = 1;
				RevitApplication.thisApp.IsHide = true;
			}
			else
			{
				this.ibtTwoPoint.ImageIndex = 0;
				RevitApplication.thisApp.IsHide = false;
			}
			this.MakeRequest(RequestId.ViewTwoPoint);
		}

		private void ibtDoorWin_Click(object sender, EventArgs e)
		{
			if (this.ibtDoorWin.ImageIndex == 0)
			{
				this.ibtDoorWin.ImageIndex = 1;
				RevitApplication.thisApp.IsHide = true;
			}
			else
			{
				this.ibtDoorWin.ImageIndex = 0;
				RevitApplication.thisApp.IsHide = false;
			}
			this.MakeRequest(RequestId.ViewDoorWin);
		}

		private void ibtWallThick_Click(object sender, EventArgs e)
		{
			if (this.ibtWallThick.ImageIndex == 0)
			{
				this.ibtWallThick.ImageIndex = 1;
				RevitApplication.thisApp.IsHide = true;
			}
			else
			{
				this.ibtWallThick.ImageIndex = 0;
				RevitApplication.thisApp.IsHide = false;
			}
			this.MakeRequest(RequestId.ViewWallThick);
		}

		private void ibtAxis_Click(object sender, EventArgs e)
		{
			if (this.ibtAxis.ImageIndex == 0)
			{
				this.ibtAxis.ImageIndex = 1;
				RevitApplication.thisApp.IsHide = true;
			}
			else
			{
				this.ibtAxis.ImageIndex = 0;
				RevitApplication.thisApp.IsHide = false;
			}
			this.MakeRequest(RequestId.ViewAxis);
		}

		protected override void OnFormClosed(FormClosedEventArgs e)
		{
			this.m_ExEvent.Dispose();
			this.m_ExEvent = null;
			this.m_Handler = null;
			this.SaveData();
			base.OnFormClosed(e);
		}

		private void EnableCommands(bool status)
		{
			foreach (object obj in this.panel1.Controls)
			{
				System.Windows.Forms.Control control = (System.Windows.Forms.Control)obj;
				if (control is Button)
				{
					control.Enabled = status;
				}
			}
		}

		private void DozeOff()
		{
			this.EnableCommands(false);
		}

		public void WakeUp()
		{
			this.EnableCommands(true);
		}

		private void MakeRequest(RequestId request)
		{
			this.m_Handler.Request.Make(request);
			this.m_ExEvent.Raise();
			this.DozeOff();
		}

		private void SaveData()
		{
			string text = Path.GetTempPath() + "DimensionHide.xml";
			XmlDocument xmlDocument = new XmlDocument();
			if (!File.Exists(text))
			{
				XmlDeclaration newChild = xmlDocument.CreateXmlDeclaration("1.0", "UTF-8", null);
				xmlDocument.AppendChild(newChild);
				XmlElement newChild2 = xmlDocument.CreateElement("Root");
				xmlDocument.AppendChild(newChild2);
				XmlNode xmlNode = xmlDocument.SelectSingleNode("Root");
				XmlElement xmlElement = xmlDocument.CreateElement("DataItem");
				xmlElement.SetAttribute("ViewID", this.viewID);
				xmlElement.SetAttribute("LocationX", base.Location.X.ToString());
				xmlElement.SetAttribute("LocationY", base.Location.Y.ToString());
				xmlElement.SetAttribute("TwoPoint", this.ibtTwoPoint.ImageIndex.ToString());
				xmlElement.SetAttribute("DoorWin", this.ibtDoorWin.ImageIndex.ToString());
				xmlElement.SetAttribute("WallThick", this.ibtWallThick.ImageIndex.ToString());
				xmlElement.SetAttribute("Axis", this.ibtAxis.ImageIndex.ToString());
				xmlNode.AppendChild(xmlElement);
				xmlDocument.AppendChild(newChild2);
			}
			else
			{
				xmlDocument.Load(text);
				XmlNodeList xmlNodeList = xmlDocument.SelectNodes("Root/DataItem");
				XmlNode xmlNode2 = null;
				foreach (object obj in xmlNodeList)
				{
					XmlNode xmlNode3 = (XmlNode)obj;
					if (xmlNode3.Attributes["ViewID"].Value == this.viewID)
					{
						xmlNode2 = xmlNode3;
						break;
					}
				}
				if (xmlNode2 == null)
				{
					XmlNode xmlNode4 = xmlDocument.SelectSingleNode("Root");
					XmlElement xmlElement2 = xmlDocument.CreateElement("DataItem");
					xmlElement2.SetAttribute("ViewID", this.viewID);
					xmlElement2.SetAttribute("LocationX", base.Location.X.ToString());
					xmlElement2.SetAttribute("LocationY", base.Location.Y.ToString());
					xmlElement2.SetAttribute("TwoPoint", this.ibtTwoPoint.ImageIndex.ToString());
					xmlElement2.SetAttribute("DoorWin", this.ibtDoorWin.ImageIndex.ToString());
					xmlElement2.SetAttribute("WallThick", this.ibtWallThick.ImageIndex.ToString());
					xmlElement2.SetAttribute("Axis", this.ibtAxis.ImageIndex.ToString());
					xmlNode4.AppendChild(xmlElement2);
				}
				else
				{
					xmlNode2.Attributes[0].Value = this.viewID;
					xmlNode2.Attributes[1].Value = base.Location.X.ToString();
					xmlNode2.Attributes[2].Value = base.Location.Y.ToString();
					xmlNode2.Attributes[3].Value = this.ibtTwoPoint.ImageIndex.ToString();
					xmlNode2.Attributes[4].Value = this.ibtDoorWin.ImageIndex.ToString();
					xmlNode2.Attributes[5].Value = this.ibtWallThick.ImageIndex.ToString();
					xmlNode2.Attributes[6].Value = this.ibtAxis.ImageIndex.ToString();
				}
			}
			xmlDocument.Save(text);
		}

		private bool ReadData(DimensionHide hide)
		{
			string text = Path.GetTempPath() + "DimensionHide.xml";
			if (!File.Exists(text))
			{
				return false;
			}
			XmlDocument xmlDocument = new XmlDocument();
			xmlDocument.Load(text);
			XmlNodeList xmlNodeList = xmlDocument.SelectNodes("Root/DataItem");
			XmlNode xmlNode = null;
			foreach (object obj in xmlNodeList)
			{
				XmlNode xmlNode2 = (XmlNode)obj;
				if (xmlNode2.Attributes["ViewID"].Value == this.viewID)
				{
					xmlNode = xmlNode2;
					break;
				}
			}
			if (xmlNode != null)
			{
				int x = Convert.ToInt32(xmlNode.Attributes[1].Value);
				int y = Convert.ToInt32(xmlNode.Attributes[2].Value);
				base.Location = new System.Drawing.Point(x, y);
				this.ibtTwoPoint.ImageIndex = Convert.ToInt32(xmlNode.Attributes[3].Value);
				this.ibtDoorWin.ImageIndex = Convert.ToInt32(xmlNode.Attributes[4].Value);
				this.ibtWallThick.ImageIndex = Convert.ToInt32(xmlNode.Attributes[5].Value);
				this.ibtAxis.ImageIndex = Convert.ToInt32(xmlNode.Attributes[6].Value);
				hide.Excute(RequestId.ViewTwoPoint, this.ibtTwoPoint.ImageIndex != 0);
				hide.Excute(RequestId.ViewDoorWin, this.ibtDoorWin.ImageIndex != 0);
				hide.Excute(RequestId.ViewWallThick, this.ibtWallThick.ImageIndex != 0);
				hide.Excute(RequestId.ViewAxis, this.ibtAxis.ImageIndex != 0);
				return true;
			}
			return false;
		}

		private void DlgDimensionHide_KeyPress(object sender, KeyPressEventArgs e)
		{
			if (e.KeyChar == '\u001b')
			{
				base.Close();
				return;
			}
		}

		private RequestHandler m_Handler;

		private ExternalEvent m_ExEvent;

		private string viewID;

		private UIApplication uiapp;
	}
}
